package com.minimalist.lindi.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.minimalist.common.entity.lindi.po.LindiPurchaseIndexRecordFile;
import com.minimalist.lindi.mapper.LindiPurchaseIndexRecordFileMapper;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 11987
 */
@Component
public class LindiPurchaseIndexRecordManager{

    @Autowired
    private LindiPurchaseIndexRecordFileMapper lindiPurchaseIndexRecordFileMapper;

    /**
     * 根据指标购买记录ID获取指标购买记录文件列表
     *
     * @param purchaseIndexRecordId 指标购买记录ID
     * @return 指标购买记录文件列表
     */
    public List<LindiPurchaseIndexRecordFile>
    getPurchaseIndexRecordFileListByPurchaseIndexRecordId(
            Long purchaseIndexRecordId){
        return lindiPurchaseIndexRecordFileMapper.selectListByQuery(
                QueryWrapper.create().eq(
                        LindiPurchaseIndexRecordFile::getPurchaseIndexRecordId
                        , purchaseIndexRecordId));
    }

    /**
     * 批量插入指标购买记录文件
     *
     * @param fileIds               文件ID列表
     * @param purchaseIndexRecordId 指标购买记录ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertPurchaseIndexRecordFiles(
            List<Long> fileIds, Long purchaseIndexRecordId){
        if (CollectionUtil.isNotEmpty(fileIds)){
            List<LindiPurchaseIndexRecordFile> list = fileIds.stream().map(f -> {
                LindiPurchaseIndexRecordFile file = new LindiPurchaseIndexRecordFile();
                file.setFileId(f);
                file.setPurchaseIndexRecordId(purchaseIndexRecordId);
                return file;
            }).toList();
            lindiPurchaseIndexRecordFileMapper.insertBatch(list);
        }
    }

    /**
     * 根据指标购买记录ID删除指标购买记录文件
     *
     * @param purchaseIndexRecordId 指标购买记录ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePurchaseIndexRecordFilesByPurchaseIndexRecordId(
            Long purchaseIndexRecordId){
        LogicDeleteManager.execWithoutLogicDelete(() ->
                lindiPurchaseIndexRecordFileMapper.deleteByQuery(
                        QueryWrapper.create().eq(
                                LindiPurchaseIndexRecordFile::getPurchaseIndexRecordId
                                , purchaseIndexRecordId))
        );
    }

    /**
     * 根据文件ID删除指标购买记录文件
     *
     * @param fileId 文件ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePurchaseIndexRecordFilesByFileId(
            Long fileId){
        LogicDeleteManager.execWithoutLogicDelete(() ->
                lindiPurchaseIndexRecordFileMapper.deleteByQuery(
                        QueryWrapper.create().eq(
                                LindiPurchaseIndexRecordFile::getFileId
                                , fileId))
        );
    }
}
